﻿using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.Data.Json;

namespace NotWindowsStoreMetro
{
    public class NotWindowsStoreDataSource
    {
        public static readonly Uri SourceUri = new Uri("http://notwindowsstore.com/");

        public async Task<List<StoreCategory>> GetStoreAppsGroupedByStoreCategories(Uri baseUri)
        {
            NotWindowsStoreDataSource dataSource = new NotWindowsStoreDataSource();
            var storeCategories = new List<StoreCategory>();

            var client = new HttpClient();
            var responseMsg = await client.GetAsync(new Uri(SourceUri, "App"));
            var responseStr = responseMsg.Content.ReadAsString();
            var categories1 = new JsonArray(responseStr).ToArray();
            IEnumerable<JsonObject> apps = categories1.Select(i => i.GetObject()).ToArray();

            var groupedCategories = apps.GroupBy(a => a.GetNamedString("Category"));
            foreach (var category in groupedCategories)
            {
                int categoryId = (int)category.First().GetNamedNumber("CategoryId");

                var storeCategory = new StoreCategory();
                storeCategory.Title = category.Key;
                storeCategory.Subtitle = "DONT SHOW ME - catsubtitle";
                storeCategory.SetImage(new Uri(SourceUri, "Category/Image/" + categoryId));
                storeCategory.Link = "http://zzz";
                storeCategory.Category = "DONT SHOW ME - cat cat";
                storeCategory.Description = "DONT SHOW ME - cat desc";
                storeCategory.Content = "DONT SHOW ME - cat content";
                storeCategories.Add(storeCategory);

                foreach (var app in category)
                {
                    int appId = (int)app.GetNamedNumber("AppId");
                    string description = app.GetNamedString("Description");
                    int downloadCount = (int)app.GetNamedNumber("DownloadCount");
                    string author = app.GetNamedString("UserName");
                    string uploadDate = app.GetNamedString("UploadDate");

                    var item = new StoreApp();
                    item.AppId = appId;
                    item.Title = app.GetNamedString("AppName");
                    item.Subtitle = "DONT SHOW ME - subtitle";
                    item.SetImage(new Uri(SourceUri, "App/Image/" + appId));
                    item.Link = "http://aaa";
                    item.Category = category.Key;
                    item.Description = description.Length > 500 ? description.Substring(0, 500) : description;
                    item.Content = description;
                    item.DownloadCount = downloadCount;
                    item.Collection = storeCategory;
                    item.Author = author;
                    item.Uploaded = uploadDate;
                    storeCategory.Add(item);
                }
            }

            return storeCategories;
        }
    }
}
